/** 
 * @Title: Bin2Text.java 
 * @Package word2vec 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月29日 下午12:00:13 
 */
package word2vec;

/** 
 * @ClassName: Bin2Text 
 * @Description: TODO 
 * @author HeYan
 * @date 2016年9月29日 下午12:00:13 
 *  
 */

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;

public class Bin2Text {

	private static final int MAX_SIZE = 100;
	private static HashMap<String, float[]> wordMap = new HashMap<String, float[]>();
	private static int words;
	private static int size;
	// private int topNSize = 4;

	private static String readString(DataInputStream dis) throws IOException {
		byte[] bytes = new byte[MAX_SIZE];
		byte b = dis.readByte();
		int i = -1;
		StringBuilder sb = new StringBuilder();
		while (b != 32 && b != 10) {
			i++;
			bytes[i] = b;
			b = dis.readByte();
			if (i == 49) {
				sb.append(new String(bytes));
				i = -1;
				bytes = new byte[MAX_SIZE];
			}
		}
		sb.append(new String(bytes, 0, i + 1));
		return sb.toString();
	}

	public static float getFloat(byte[] b) {
		int accum = 0;
		accum = accum | (b[0] & 0xff) << 0;
		accum = accum | (b[1] & 0xff) << 8;
		accum = accum | (b[2] & 0xff) << 16;
		accum = accum | (b[3] & 0xff) << 24;
		return Float.intBitsToFloat(accum);
	}

	public static float readFloat(InputStream is) throws IOException {
		byte[] bytes = new byte[4];
		is.read(bytes);
		return getFloat(bytes);
	}

	@SuppressWarnings("unused")
	public static void main(String[] args) throws Exception {
		String path = "/var/server/java/heyan/w2v/trunk/vectors1122.bin";
		DataInputStream dis = null;
		BufferedInputStream bis = null;
		double len = 0;
		float vector = 0;
		try {
			bis = new BufferedInputStream(new FileInputStream(path));
			dis = new DataInputStream(bis);
			// //读取词数
			words = Integer.parseInt(readString(dis));
			// //大小
			size = Integer.parseInt(readString(dis));

			String word;
			float[] vectors = null;
			File file = new File("/var/server/java/heyan/vectors1122.txt");
			if (file == null) {
				file.createNewFile();
			}
			BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
			for (int i = 0; i < words; i++) {
				word = readString(dis);
				fw.append(word + "\t");
				vectors = new float[size];
				len = 0;
				for (int j = 0; j < size; j++) {
					vector = readFloat(dis);
					len += vector * vector;
					vectors[j] = (float) vector;
				}
				len = Math.sqrt(len);

				for (int j = 0; j < vectors.length; j++) {
					vectors[j] = (float) (vectors[j] / len);
					fw.append(vectors[j] + "\t");
				}
				wordMap.put(word, vectors);
				fw.append("\n");
				dis.read();
			}
			fw.flush();
			fw.close();

		} finally {
			bis.close();
			dis.close();
		}
	}
}
